package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.aop.CacheFind;
import com.jt.mapper.ItemCatMapper;
import com.jt.pojo.Item;
import com.jt.pojo.ItemCat;
import com.jt.util.ObjectMapperUtil;
import com.jt.vo.EasyUITree;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.ShardedJedis;

import java.util.ArrayList;
import java.util.List;
@Service
public class ItemCatServiceImpl implements ItemCatService{
    @Autowired
    private ItemCatMapper itemCatMapper;
    @Override
    public ItemCat queryItemName(Long ItemCatId) {

        return itemCatMapper.queryItemName(ItemCatId);
    }
    @CacheFind(key = "ITEM_CAT_PARENT",seconds = 7*24*60*60)
    @Override
    public List<EasyUITree> findItemCatList(Long parentId) {
       List<EasyUITree> treeList=new ArrayList<>();
       List<ItemCat> list=itemCatMapper.selectByParentId(parentId);
       for (ItemCat item:list){
           long id= item.getId();
           String text=item.getName();
           String status=item.getIsParent()?"closed":"open";
            EasyUITree easyUITree=new EasyUITree(id,text,status);
            treeList.add(easyUITree);
       }
        return treeList;
    }


    /*
    * @Param parentId
    * @Return null? List<EasyUITree>*/
//    @Autowired(required = false)//暂时不注入，调用才注入,这个对象可以没有
//    @Lazy
//    private Jedis jedis;
    @Autowired
    private ItemCatServiceImpl itemCatService;
    /*@Autowired(required = false)
    private ShardedJedis jedis;*/
    @Autowired
    private JedisCluster jedis;
    @Override
    public List<EasyUITree> findItemCatCache(long parentId) {
        String key="ITEM_CAT_PARENTID"+parentId;
        String turn="";
        List<EasyUITree> list=null;
        if (!jedis.exists(key)){
            list=itemCatService.findItemCatList(parentId);
            String json= ObjectMapperUtil.toJSON(list);
            jedis.set(key,json);
            return list;
        }else {
            turn= jedis.get(key);
            return ObjectMapperUtil.toObj(turn,list.getClass());
        }


    }
}